# Define a variable for classpath
SO = linux
# SO = windows
ARCH = x86
TYPE = 32

CLASS_PATH = ../bin
INCLUDE = -I"$(JAVA_HOME)/include" -I"$(JAVA_HOME)/include/win32" -I"$(JAVA_HOME)/include/linux"

PACKAGE = org.jsux
PACKAGE_C = org_jsux
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)/org/jsux/

OPTIONS_x86_32 = -O2 -m32
OPTIONS_x86_64 = -O2 -m64
OPTIONS = $(OPTIONS_$(ARCH)_$(TYPE))

CFLAGS_linux = -fPIC
CFLAGS_windows =
CFLAGS = $(OPTIONS) $(CFLAGS_$(SO))

LDFLAGS_linux = 
LDFLAGS_windows = -Wl,--add-stdcall-alias
LDFLAGS = $(OPTIONS) $(LDFLAGS_$(SO))

LIBNAME_PRE_linux = lib
LIBNAME_PRE_window = 
LIBNAME_PRE = $(LIBNAME_PRE_$(SO))

LIBNAME_POS_linux = .so
LIBNAME_POS_windows = .dll
LIBNAME_POS = $(LIBNAME_POS_$(SO))

LIBNAME = $(LIBNAME_PRE)SerialPort_$(ARCH)_$(TYPE)$(LIBNAME_POS)

all : clean build

# $@ matches the target, $< matches the first dependancy
build : SerialPort.o impl.o
	gcc $(LDFLAGS) -shared -o $(LIBNAME) $^
	mkdir -p ../bin/native
	cp $(LIBNAME) ../bin/native/
	jar cfe SerialPort.jar $(PACKAGE).SerialPort -C ../bin .
	mkdir -p ../build
	mv SerialPort.jar ../build/

# $@ matches the target, $< matches the first dependancy
SerialPort.o : SerialPort.c SerialPort.h
	gcc $(CFLAGS) $(INCLUDE) -D$(SO) -c $< -o $@

# $@ matches the target, $< matches the first dependancy
impl.o : impl.c impl.h
	gcc $(CFLAGS) $(INCLUDE) -D$(SO) -c $< -o $@

# $* matches the target filename without the extension
SerialPort.h : SerialPort.class
	javah -classpath $(CLASS_PATH) org.jsux.$*

cleanAll : clean
	rm -rf ../bin/native ../build SerialPort_*.dll SerialPort_*.so

clean :
	rm -rf  $(PACKAGE_C)_SerialPort*.h SerialPort.o impl.o $(LIBNAME)

